package controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import bean.Fund;
import bean.LoggedUser;
import bean.Model;
import bean.UserType;
import dao.CustomerDAO;
import dao.FundDAO;

/*
 * 
 * Created By Sahil Thakkar for Buy Fund  Usecase
 */

public class OpenBuyFundAction extends Action {
	
	private FundDAO fundDAO;
	private CustomerDAO customerDAO;
	public OpenBuyFundAction(Model model) {
		this.fundDAO = model.getFundDAO();
		this.customerDAO = model.getCustomerDAO();
	}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return "openBuyFund.do";
	}

	@Override
	public String perform(HttpServletRequest request) {
		// TODO Auto-generated method stub
		List<String> errors = new ArrayList<String>();
		request.setAttribute("errors", errors);
		List<Fund> fundList = null;
		
		try {
			
			HttpSession session     = request.getSession(false);
			LoggedUser user = (LoggedUser) session.getAttribute("user");
			long balance = customerDAO.getCustomerCash(""+user.getId());
			
			fundList = fundDAO.getAllFund();

			request.setAttribute("fundList", fundList);
			request.setAttribute("balance", util.Util.showMoney(balance));				
			
			return "openBuyFund.jsp";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error.jsp";
		}
	}

	 @Override
		public UserType getType() {
	    	return UserType.Customer;
		}
}
